<template>
	<div class="applicationList">
		<Card>
			<Table :columns="columns" :data="tableData"></Table>
			<Page :total="count" show-elevator show-total :page-size="limit" style="margin:35px 0" @on-change="handlePage"/>
		</Card>
	</div>
</template>

<script>
	export default {
		data () {
			return {
				columns: [
					{
						title: '应用名称',
						key: 'podName'
					},
					{
						title: '内核',
						key: 'otherInfo',
						render: (h, params) => {
							var cpu = params.row.otherInfo.cpu.usage.per_cpu_usage
							return h('span', cpu.length)
						}
					},
					{
						title: '内存',
						key: 'otherInfo',
						render: (h, params) => {
							var memory = params.row.otherInfo.memory.working_set / 1048576
							if (memory > 1024) {
								memory = parseFloat((memory / 1024).toFixed(2)) + 'GB'
							} else {
								memory = parseFloat(memory.toFixed(2)) + 'MB'
							}
							return h('span', memory)
						}
					},
					{
						title: '应用分组',
						key: 'nsName'
					},
					{
						title: '状态',
						key: 'stats',
						render: (h, params) => {
							var status = JSON.stringify(params.row.stats)
							if (status.indexOf('running') !== -1) {
								return h('span', '运行中')
							} else {
								return h('span', '故障')
							}
						}
					},
					{
						title: '操作',
						key: 'handle',
						render: (h, params) => {
							return h('div', [
								h('Button', {
									props: {
										type: 'primary',
										size: 'small'
									},
									style: {
										marginRight: '5px',
										background: '#d00210',
										borderColor: '#d00210',
										color: '#fff!important'
									},
									on: {
										click: () => {
											sessionStorage.containerName = params.row.podName
											this.$router.push({
												name: 'application'
											})
										}
									}
								}, '查看应用详情')
							])
						}
					}
				],
				tableData: [],
				page: 1,
				limit: 5,
				count: 0
			}
		},
		methods: {
			getList () {
				this.$api.get(0, '/kuber/con/page/info?page=' + this.page + '&limit=5&owner=' + this.user.userName, {}, data => {
					if (data.data.code === 0) {
						this.tableData = data.data.data.data
						this.count = data.data.data.count
					}
				})
			},
			// 分页跳转
			handlePage (value) {
				this.page = value
				this.getList()
			}
		},
		mounted () {
			this.user = JSON.parse(sessionStorage.user)
			this.getList()
		}
	}
</script>

<style>
</style>
